﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;

namespace WebCoreAPI.Config
{
    public class JWTConfig
    {
        public static void InjectConfig(WebApplicationBuilder builder)
        {
            IServiceCollection services = builder.Services;
            IConfigurationSection cofSection= builder.Configuration.GetSection("JWT");
            services.Configure<JWTOptions>(cofSection);
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(j =>
            {
                var jwtOpt = cofSection.Get<JWTOptions>();
                byte[] keyBytes = Encoding.UTF8.GetBytes(jwtOpt.SigningKey);
                var secKey=new SymmetricSecurityKey(keyBytes);
                j.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = false,
                    ValidateAudience = false,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = secKey
                };
            });

        }
    }
    public class JWTOptions
    {
        public String SigningKey { get; set; }
        public int ExpireSeconds { get; set; }
    }
}
